package com.campus.controller;

import com.campus.entity.ResultBody;
import com.campus.entity.System.SysUser;
import com.campus.entity.Test4;
import com.campus.service.SysUserService;
import com.campus.service.impl.Test4ServiceImpl;
import com.campus.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.bouncycastle.asn1.x509.X509ObjectIdentifiers.id;

/**
 * 测试
 *
 * @author Alaskaboo
 * @since 2022-10-23 14:42:37
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/user/list")
    // @PreAuthorize("hasRole('ROLE_admin2')")
    @PreAuthorize("hasAuthority('system:user2:list')")
    public ResultBody userList(@RequestHeader(required = false) String token) {
        if (StringUtil.isNotEmpty(token)) {
            Map<String, Object> resutlMap = new HashMap<>();
            List<SysUser> userList = sysUserService.list();
            resutlMap.put("userList", userList);
            return ResultBody.ok().data(resutlMap);
        } else {
            return ResultBody.error().code(401).message("没有权限访问");
        }

    }
    //
    //    @RequestMapping("/login")
    //    public ResultBody login(){
    //        String token = JwtUtils.genJwtToken("java1234");
    //        return ResultBody.ok().data("token",token);
    //    }

    /**
     * 作业测试
     * @return
     */
    @Autowired
    private Test4ServiceImpl test4Service;

    @PutMapping
    @CacheEvict(value = "modifyInfo", allEntries =true)
    public ResultBody loginCommission(@RequestBody Test4 test4){
        test4Service.updateById(test4);
        return ResultBody.ok().message("修改成功").data("user",test4).code(2000);
    }

    @GetMapping("/{id}")
    @Cacheable(value = "modifyInfo", key = "#id")
    public ResultBody queryAccount(@PathVariable Integer id){
        Test4 modifyInfo = test4Service.getById(id);
        return ResultBody.ok().message("查询成功").data("user", modifyInfo);
    }
}
